//
//  tvvovo.cpp
//  complexidade
//  http://br.spoj.pl/problems/TV/
//  Created by Bruno Dias on 01/06/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#include <iostream>

using namespace std;
#define MAX 1001

int main(){
    int l, c, teste = 1;
    int matrix[MAX][MAX];
    while ((cin >> l >> c) && l != 0 && c != 0) {
        int x, y, deslx = 0, desly = 0;
        for(int i = 0; i < l; i++ ) {
            for(int j = 0; j < c; j++ ) {
                scanf( "%d", &matrix[i][j] );
            }
        }
        while ((cin >> x >> y) && (x || y)) {
            deslx += y;
            desly += x;
        }
        deslx = 2*l + (deslx % l);
        desly = 2*c - (desly % c);
        cout << "Teste " << teste++ << "\n"; 
        for (int i = deslx - l; i < deslx; i++) {
            for (int  j = desly - c; j < desly; j++) {
                int a = i % l, b = j % c;
                printf("%d ", matrix[a][b]);
            }
            cout << "\n"; 
        }
        cout << "\n";
    }
}


